package thread;

import cn.hutool.core.thread.NamedThreadFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author ycz
 * @date 2021/5/31
 * @description 线程池和Callable
 *
 *      // 存放工作线程的集合
 *     private final HashSet<Worker> workers = new HashSet<Worker>();
 *     // Worker线程池核心
 *     Worker implements Runnable{
 *            final Thread thread; // 具体工作的线程
 *            Runnable firstTask; // 第一次要执行任务
 *            volatile long completedTasks;
 *     }
 *
 * 拒绝策略
 * AbortPolicy(默认)：直接抛出RejectedExecutionException异常
 * CallerRunsPolicy：再任务被拒绝添加后，会再次调用execute()来执行被拒绝的任务
 * DiscardOldestPolicy：抛弃队列中等待最久的任务
 * DiscardPolicy：直接丢弃任务，不予处理也不抛出异常
 */
public class ThreadPollAndCallAble implements Callable<Long> {

    private long start;
    private long end;

    static {
        System.out.println("this is static");
    }

    public ThreadPollAndCallAble() {
        System.out.println("this is empty thread.ThreadPollAndCallAble");
    }

    public ThreadPollAndCallAble(long start, long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public Long call() throws Exception {
        long sum = 0L;
        while (sum < 10000) {
            sum += 1;
        }
        return sum;
    }

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, Integer.MAX_VALUE,
                TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10000), new NamedThreadFactory("threadPoolExecutor", false));
        System.out.println(threadPoolExecutor.getCorePoolSize());

        System.out.println(new BigDecimal("9").negate());

        ExecutorService executorService = Executors.newFixedThreadPool(2);
        ExecutorService executorService1 = Executors.newSingleThreadExecutor();
        ExecutorService executorService2 = Executors.newCachedThreadPool();
        ExecutorService executorService3 = Executors.newWorkStealingPool();
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);

        executorService.execute(new Runnable() {
            @Override
            public void run() {

            }
        });


//        long start1=System.currentTimeMillis();
//        List<Future<Long>> ans = new CopyOnWriteArrayList<>();
//        for (int i=0;i<10;i++){
//            Future<Long> future = threadPoolExecutor.submit(new thread.ThreadPollAndCallAble(100, 1000000));
//            ans.add(future);
//        }

    }


}
